import requests
import time

class WebhookSender:
    """
    This class is responsible for sending messages to DingTalk using webhook URLs.
    """

    def send_to_dingtalk(self, message: str, webhook_url: str) -> None:
        """
        Sends a message to DingTalk using the provided webhook URL.

        Args:
            message (str): The message to send.
            webhook_url (str): The DingTalk webhook URL to send the message to.

        Returns:
            None
        """
        if not webhook_url or not message:
            raise ValueError("The webhook URL and message must not be empty.")

        headers = {"Content-Type": "application/json"}
        payload = {"msgtype": "text", "text": {"content": message}}
        retry_count = 0
        max_retries = 3
        retry_delay = 10  # seconds

        while retry_count < max_retries:
            try:
                response = requests.post(webhook_url, json=payload, headers=headers)
                if response.status_code == 200:
                    break  # Success, exit loop
                elif response.status_code == 429:
                    # Handle rate limiting
                    time.sleep(retry_delay)
                    retry_count += 1
                    continue  # Retry
                else:
                    # Handle other HTTP errors
                    raise Exception(f"Failed to send message to DingTalk. HTTP status code: {response.status_code}")
            except requests.exceptions.RequestException as e:
                raise Exception(f"Network-related error occurred: {e}")

        if retry_count == max_retries:
            raise Exception("Failed to send message after multiple retries due to rate limiting.")
